package com.fndsoft.zheshangmeap.net.receive;

import org.json.JSONException;
import org.json.JSONObject;

import com.fndsoft.zheshangmeap.domain.ThreadMessage;
import com.fndsoft.zheshangmeap.net.HttpReturnMessage;
import com.fndsoft.zheshangmeap.net.OperateCode;
import com.fndsoft.zheshangmeap.util.ValueUtil;

/**
 * 用户相关接口的结果处理类
 * 
 * @author LiangXiangJu
 * @creationTime 2014-3-13 下午1:22:29
 * @version
 */
public class UserReceiveService extends BaseReceiveService {
	/**
	 * 后台接收到服务端数据处理
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-3-13 下午5:23:46
	 * @version
	 * @param httpReturnMessage
	 * @return
	 */
	public static boolean receiveData(HttpReturnMessage httpReturnMessage) {
		OperateCode code = httpReturnMessage.getThreadMessage()
				.getOperateCode();
		boolean result = true;
		switch (code) {
		case i_login: // 用户登录
			operateUserLogin(httpReturnMessage);
			break;
		default:
			result = false;
			break;
		}
		return result;
	}

	/**
	 * 用户登录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-3-13 下午1:50:20
	 * @version
	 * @param httpRerurnMessage
	 */
	private static void operateUserLogin(HttpReturnMessage httpReturnMessage) {
		String jsonContent = httpReturnMessage.getResultContent();

		if (ValueUtil.isEmpty(jsonContent)) {
			return;
		}

		ThreadMessage threadMessage = httpReturnMessage.getThreadMessage();
		
		if (!threadMessage.isReceviceIsSucces()) {
			return;
		}
		threadMessage.setStringData1(null);

		try {
			JSONObject jsonObject = new JSONObject(jsonContent);
			JSONObject jsonStatus = jsonObject.getJSONObject("status");

			// 登录状态(loginState)
			if (jsonStatus.isNull("loginState")) {
				return;
			}
//			String loginState = jsonStatus.getString("loginState");
//			int resId = R.string.error_unknownerror;
//			if ("100".equals(loginState)) {
//				resId = -1;
//			} else if ("200".equals(loginState)) {
//				resId = R.string.login_state_200;
//			} else if ("300".equals(loginState)) {
//				resId = R.string.login_state_300;
//			}
//			threadMessage.setReceviceErrorResCode(resId);

			threadMessage.setStringData1(jsonStatus.getString("loginState"));
		} catch (JSONException e) {
		}
	}

}
